import { PhoneNumberComponent } from './phone-number/phone-number.component';
import { PersonEditDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
  PagedListingComponentBase,
  PagedRequestDto
} from 'shared/paged-listing-component-base';
import {
  PersonListDto,
  PersonServiceProxy,
  PagedResultDtoOfPersonListDto
} from '@shared/service-proxies/service-proxies';
import { CreatePersonComponent } from '@app/persons/create-person/create-person.component';
import { EditPersonComponent } from '@app/persons/edit-person/edit-person.component';

@Component({
  selector: 'app-persons',
  templateUrl: './persons.component.html',
  animations: [appModuleAnimation()]
})
export class PersonsComponent extends PagedListingComponentBase<PersonListDto>
  implements OnInit {
  @ViewChild('createPersonModal')
  createPersonModal: CreatePersonComponent;
  @ViewChild('editPersonModal')
  editPersonModal: EditPersonComponent;

  @ViewChild('phoneNumberModal')
  phoneNumberModal: PhoneNumberComponent;

  active = false;

  filter = '';
  persons: PersonListDto[] = [];
  constructor(injector: Injector, private _personService: PersonServiceProxy) {
    super(injector);
  }
  list(
    request: PagedRequestDto,
    pageNumber: number,
    finishedCallback: Function
  ): void {
    this._personService
      .getPagedPersons(
        this.filter,
        'Id',
        request.maxResultCount,
        request.skipCount
      )
      .pipe(
        finalize(() => {
          finishedCallback();
        })
      )
      .subscribe((result: PagedResultDtoOfPersonListDto) => {
        this.persons = result.items;
        this.showPaging(result, pageNumber);
      });
  }

  keyPress($event): void {
    if ($event.charCode === 13) {
      this.refresh();
    }
  }

  delete(person: PersonListDto): void {
    abp.message.confirm(
      this.l('DeletePerson{0}', person.name),
      this.l('PermanentlyDeleteThisPerson'),
      (result: boolean) => {
        if (result) {
          this._personService
            .deletePerson(person.id)
            .subscribe(() => {
              abp.notify.info(this.l('DeletePerson:{0}', person.name));
              this.refresh();
            });
        }
      }
    );
  }

  deletePhoneNumber(id: number, number: string): void {
    abp.message.confirm(
      this.l('DeletePhoneNumber{0}', number),
      this.l('PermanentlyDeleteThisPhoneNumber'),
      (result: boolean) => {
        if (result) {
          this._personService
            .deletePhoneNumberAsync(id)
            .subscribe(() => {
              abp.notify.info(this.l('DeletePhoneNumber:{0}', number));
              this.refresh();
            });
        }
      }
    );
  }

  // Show Modals
  createPerson(): void {
    this.createPersonModal.show();
  }

  editPerson(person: PersonEditDto): void {
    this.editPersonModal.show(person.id);
  }

  addPhoneNumber(person: PersonEditDto): void {
    this.phoneNumberModal.show(person.id);
  }
}
